package com.boc.langxiang.ui.activity.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.boc.langxiang.R;
import com.boc.langxiang.entity.events.LoginSuccessEvent;
import com.boc.langxiang.ui.fragment.common.LoginFragment;
import com.boc.langxiang.ui.fragment.common.RegistFragment;
import com.boc.langxiang.ui.fragment.index.IndexFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册登录
 */
public class RegistAndLoginActivity extends AppCompatActivity {

    @BindView(R.id.img_login)
    ImageView mImgLogin;
    @BindView(R.id.login)
    RelativeLayout mLogin;
    @BindView(R.id.img_regist)
    ImageView mImgRegist;
    @BindView(R.id.regist)
    RelativeLayout mRegist;

    private final String TAG_FRAGMENT_LOGIN = "login";
    private final String TAG_FRAGMENT_REGISTE = "registe";

    private Fragment lastFragment = null;
    private int lastChecked = R.id.login;
    private LoginFragment mLoginFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist_and_login);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        mImgLogin.setVisibility(View.VISIBLE);

        mLoginFragment = LoginFragment.newInstance();
        changeFragment(TAG_FRAGMENT_LOGIN);
    }

    private void changeFragment(String tag) {
        Fragment fragment = getSupportFragmentManager()
                .findFragmentByTag(tag);
        if (fragment == null) {
            fragment = getFragmentByTag(tag);
            addFragment(fragment, tag);
        } else {
            showFragment(fragment);
        }
        lastFragment = fragment;
    }

    private Fragment getFragmentByTag(String tag) {
        if (TextUtils.isEmpty(tag)) {
            return mLoginFragment;
        }
        if (tag.equals(TAG_FRAGMENT_LOGIN)) {
            return mLoginFragment;
        } else if (tag.equals(TAG_FRAGMENT_REGISTE)) {
            return RegistFragment.newInstance();
        }
        return IndexFragment.newInstance();
    }

    private void addFragment(Fragment fragment, String tag) {
        FragmentTransaction trans = getSupportFragmentManager()
                .beginTransaction();
        if (lastFragment != null) {
            trans.hide(lastFragment);
        }
        trans.add(R.id.content, fragment, tag);
        trans.commitAllowingStateLoss();
    }

    private void showFragment(Fragment fragment) {
        FragmentTransaction trans = getSupportFragmentManager()
                .beginTransaction();
        if (lastFragment != null) {
            trans.hide(lastFragment);
        }
        trans.show(fragment);
        trans.commitAllowingStateLoss();
    }

    @OnClick({R.id.login, R.id.regist})
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.login:
                mImgLogin.setVisibility(View.VISIBLE);
                mImgRegist.setVisibility(View.GONE);
                if (lastChecked != R.id.index) {
                    changeFragment(TAG_FRAGMENT_LOGIN);
                    lastChecked = id;
                }
                break;
            case R.id.regist:
                mImgLogin.setVisibility(View.GONE);
                mImgRegist.setVisibility(View.VISIBLE);
                if (lastChecked != R.id.index) {
                    changeFragment(TAG_FRAGMENT_REGISTE);
                    lastChecked = id;
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mLoginFragment != null) {
            mLoginFragment.onActivityResult1(requestCode, resultCode, data);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginSuccessEvent(LoginSuccessEvent event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
